<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\User;

use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class IndexController extends CommonController
{
	public function index()
	{
		return view('admin.index');
    }

	public function info()
	{
		return view('admin.info');
    }

    //更改管理员密码
	public function pass()
	{
		$errors = array();
		if ($input = Input::all()) {
			$rules = [
				'password' => 'required | between: 6, 20 | confirmed',
			];
			$message = [
				'password.required' => '新密码不能为空',
				'password.between' => '新密码必须为6-20位',
				'password.confirmed' => '确认密码不一致',
			];
			$validator = Validator::make($input, $rules, $message);

			if ($validator->passes()) {
				$user = User::first();
				$_password = Crypt::decrypt($user->user_password);
				if ($input['password_o'] == $_password) {
					$user->user_password = Crypt::encrypt($input['password']);
					$user->update();
					$errors[0] = '密码修改成功';
					return back()->withErrors($errors);
				} else {
					$errors[0] = '原始密码错误';
					return back()->withErrors($errors);
				}
			} else {
				return back()->withErrors($validator);
			}
		} else {
			return view('admin.pass');
		}
	}
}
